/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Classes;

/**
 *
 * @author ward
 */
public class HelperGoogleChart {
        String [] colors =
        {
            "4572a7",
            "aa4643",
            "89a54e",
            "71588f",
            "4198af",
            "db843d",
            "9999ff",
            "993366",
            "ffffcc",
            "ccffff",
            "660066",
            "ff8080",
            "0066cc",
            "ccccff"
        };
        String [] alertColors =
        {
            "4572a7",
            "aa4643",
            "89a54e",
            "71588f",
            "4198af",
            "db843d",
            "9999ff",
            "993366",
            "ffffcc",
            "ccffff",
            "660066",
            "ff8080",
            "0066cc",
            "ccccff"
        };
    
    Double overallMax = Double.MAX_VALUE;
    
    public HelperGoogleChart() {
    }
    
    public void SetOverallMax( String val )
    {
        if( val.length() > 0 )
            overallMax = Double.parseDouble( val );
    }
    public String GetRowLabels( AE.Page currentPage  )
    {
        String retVal = "";
        
        for( int rowIndex = 0, rowCount = currentPage.GetRowCount(); 
            rowIndex < rowCount ; rowIndex++ )
        {
            retVal += "|" + currentPage.GetRowLabelValue(rowIndex);
        }
        return retVal ; // "|Mar|Apr|May|June|July|" ;
    }
    public String GetColumnLabels( AE.Page currentPage )
    {
        String retVal = "chxl=0:";
        
        for( int numericColumnIndex = 0,  numericCount = currentPage.GetNumericColumnCount() ; 
                    numericColumnIndex < numericCount ; numericColumnIndex++ )
        {
            retVal += "|" + currentPage.GetNumericColumnName(numericColumnIndex);
        }
        
        retVal += "||" + overallMax.toString();
        
        return retVal ; // "||50+Kb"; 
    }
    
    public String GetPieLabelsColumns( AE.Page currentPage )
    {
        String retVal = "chl=";
        for( int numericColumnIndex = 0,  numericCount = currentPage.GetNumericColumnCount() ; 
                    numericColumnIndex < numericCount ; numericColumnIndex++ )
        {
            if( numericColumnIndex  > 0 )
                retVal += "|" ;
            retVal += currentPage.GetNumericColumnName(numericColumnIndex);
        }
        return retVal;
    }

    public String GetPieLabelsRows( AE.Page currentPage, int columnIndex )
    {
        String retVal = "chl=";
        for( int rowIndex = 0,  rowCount = currentPage.GetRowCount(); 
                    rowIndex < rowCount ; rowIndex++ )
        {
            if( rowIndex  > 0 )
                retVal += "|" ;
            retVal += currentPage.GetRowLabelValue(rowIndex);
            
            if( columnIndex > -1 )
            {
               retVal += " " + currentPage.GetNumericCellContents(rowIndex, columnIndex, true);
            }
        }
        return retVal;
    }

    public String GetMarkersColumn( AE.Page currentPage, int numericColumnIndex )
    {
        String[] markers = { "c", "d", "o", "s", "a", "v", "h", "x", "c", "d", "o", "s", "a" };  // different googlechart markers , need up to 10
        String retVal = "chm="; 
        for( int rowIndex = 0, rowCount = currentPage.GetRowCount(); 
            rowIndex < rowCount ; rowIndex++ )
        {
            String alertLevel = currentPage.GetAlertLevel(rowIndex, numericColumnIndex);
            int intAlertLevel = Integer.parseInt(alertLevel);
            
            if( intAlertLevel > -1 && intAlertLevel < 10 )
            {
                if( rowIndex > 0 )
                    retVal += "|";
            
                retVal += markers[intAlertLevel] + "," ;
                retVal += alertColors[ intAlertLevel ];
                // data set index
                retVal += "," + "0" ;

                // rowIndex to identify the data point
                retVal += "," + Double.toString(rowIndex);

                // size of marker
                retVal += ",10.0";
            }
        }        
        return retVal;
    }
    public String GetDataRowNames( AE.Page currentPage )
    {
        String series = "chdl=";
        for( int rowIndex = 0, rowCount = currentPage.GetRowCount(); 
            rowIndex < rowCount ; rowIndex++ )
        {
            if( rowIndex > 0 )
                series += "|";
            series += currentPage.GetRowLabelValue(rowIndex);
        }
        return series;
    }

    public String GetDataRowsColors( AE.Page currentPage )
    {
        String series = "chco=";
        int colorIndex = 0;
        for( int rowIndex = 0, rowCount = currentPage.GetRowCount(); 
            rowIndex < rowCount ; rowIndex++ )
        {
            if( rowIndex > 0 )
                series += ",";
            String thisColor = colors[ colorIndex ];
            
            series += thisColor;
            
            if( ++colorIndex == colors.length )
                colorIndex = 0;
        }
        return series;
    }
    public String GetDataColumnNames( AE.Page currentPage )
    {
        String series = "chdl=";
        for( int colIndex = 0, colCount = currentPage.GetNumericColumnCount();
                    colIndex < colCount; colIndex++ )
        {
            if( colIndex > 0 )
                series += "|";
            
            series += currentPage.GetNumericColumnName(colIndex);
        }
        return series;
    }
    public String GetDataAllRows( AE.Page currentPage )
    {
        String series = "chd=s:";
        for( int rowIndex = 0, rowCount = currentPage.GetRowCount(); 
            rowIndex < rowCount ; rowIndex++ )
        {
            if( rowIndex > 0 )
                series += ",";
            series += GetNormalizedDataFromRow( currentPage, rowIndex );
        }
        return series;
    }
    public String GetDataColumns( AE.Page currentPage )
    {
        String series = "chd=s:";
        for( int colIndex = 0, colCount = currentPage.GetNumericColumnCount();
                    colIndex < colCount; colIndex++ )
        {
            if( colIndex > 0 )
                series += ",";
            
            series += GetNormalizedDataFromColumn( currentPage, colIndex );
        }
        return series;
    }
    public String GetDataColumnsColors( AE.Page currentPage ) //  "chco=ff0000,00ff00";
    {
        int colorIndex = 0;
        
        String series = "chco=";
        for( int colIndex = 0, colCount = currentPage.GetNumericColumnCount();
                    colIndex < colCount; colIndex++ )
        {
            String thisColor = colors[ colorIndex ];
            
            if( colIndex > 0 )
                series += ",";
            series += thisColor;
            
            if( ++colorIndex == colors.length )
                colorIndex = 0;
        }
        return series;
    }
    
    public String GetData( AE.Page currentPage, boolean columns, int position )
    {
        String retVal = "chd=s:"; // s:ABCDEFGHI";
        if( columns )
            retVal += GetNormalizedDataFromColumn(currentPage, position ); // "s:ABCDEFGHI";
        else
            retVal += GetNormalizedDataFromRow( currentPage, position ); 
        
        return retVal;
    }

    private String GetNormalizedDataFromColumn( AE.Page currentPage, int columnIndex )
    {
        // return "chd=s:helloworld";
        String retVal = ""; // "chd=s:"; // s:ABCDEFGHI";

        Double max = overallMax;
        
        if( max == Double.MAX_VALUE )
        {
            String strMax = currentPage.GetAggregateValue("column", columnIndex, "maximum", false );
            if( strMax.length() > 0 )
            {
                max = Double.parseDouble(strMax);
            }
        }
        
        for( int rowIndex = 0, rowCount = currentPage.GetRowCount(); 
            rowIndex < rowCount ; rowIndex++ )
        {
            retVal += "";

            String strVal = currentPage.GetNumericCellContents(rowIndex, columnIndex, false );
            if( strVal.length() > 0 )
            {
                Double val = Double.parseDouble(strVal);
                if( val > 0 )
                    retVal += simpleEncode( val, max );
                else
                    retVal += "A";
            }
            else
                retVal += "A";
        }            
        

        return retVal;
    }
    
    private String GetNormalizedDataFromRow( AE.Page currentPage, int rowIndex )
    {
        String retVal = "";

        Double max = overallMax;
        
        if( max == Double.MAX_VALUE )
        {
            String strMax = currentPage.GetAggregateValue("row", rowIndex, "maximum", false );
            if( strMax.length() > 0 )
            {
                max = Double.parseDouble(strMax);
            }
        }
        
        for( int columnIndex = 0, columnCount = currentPage.GetNumericColumnCount(); 
            columnIndex < columnCount ; columnIndex++ )
        {
            retVal += "";

            String strVal = currentPage.GetNumericCellContents(rowIndex, columnIndex, false );
            if( strVal.length() > 0 )
            {
                Double val = Double.parseDouble(strVal);
                if( val > 0 )
                    retVal += simpleEncode( val, max );
                else
                    retVal += "A";
            }
            else
                retVal += "A";
        }            
        return retVal;
    }
    
    public String GetChartTitle( AE.Page currentPage, boolean columns, int positionIndex, boolean pageCaption, boolean positionCaption ) 
    {
        String retVal = "chtt=" ;
        if( pageCaption ) 
            retVal += currentPage.GetPageCaption() ;

        if( positionCaption )
        {
            if( pageCaption )
                retVal += "|" ;
            if( columns )
            {
                retVal += currentPage.GetNumericColumnName(positionIndex);
            }
            else
            {
                retVal += currentPage.GetRowLabelValue(positionIndex);
            }
        }
        return retVal;
    }
    
    private String simpleEncode( Double value, Double max )
    {
        String simpleEncoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        
        if( max > 0 )
        {
            Long index = Math.round( (value / max) * simpleEncoding.length() - 1 );
            if( index > -1 && index < simpleEncoding.length() )
            {
                if( index < Integer.MAX_VALUE )
                {
                    int intIndex = index.intValue();
                    return simpleEncoding.substring( intIndex, intIndex + 1 );
                }
            }
        }
        return "A";
    }

    public String GetSize( String xAxis, String yAxis )
    {
        return "chs=" + xAxis + "x" + yAxis;
    }
    public String GetType()
    {
        return "cht=" + "lc";
    }
}
